#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <math.h>
#include <algorithm>
#include <map>
using namespace std;

bool compare(map<string,int>&a1,map<string,int>&a2){
    for(auto ite = a2.begin();ite!=a2.end();ite++){
        if(a1.find(ite->first)!=a1.end()){
            if(a1[ite->first]!=ite->second) return false;
        }else return false;
    }
//    cout<<"test"<<endl;
//    for(auto it=a2.begin();it!=a2.end();it++){
//        cout<<it->first<<","<<it->second<<"\n";
//    }
    return true;
}

vector<int> findSubstring(string s, vector<string>& words) {
    if(s.empty() || words.empty())return {};
    map<string,int>hush1;
    map<string,int>hush2;
    vector<int>result;
    short wordLength = words[0].size();
    short size = s.size();
    short allLength = wordLength * words.size();
    string temp;

    for(auto temp:words){
        hush1[temp] = hush1.find(temp)==hush1.end()?1:hush1[temp]+1;
    }

//    for(auto it=hush1.begin();it!=hush1.end();it++){
//        cout<<it->first<<","<<it->second<<"\n";
//    }

    for(short i=0;i<=size-allLength;i++){
        hush2.clear();
        temp=s.substr(i,allLength);
        for(short j=0;j<allLength;j+=wordLength){
            hush2[temp.substr(j,wordLength)] = hush2.find(temp.substr(j,wordLength))==hush2.end()?1:hush2[temp.substr(j,wordLength)]+1;
        }
        if(compare(hush1,hush2))result.push_back(i);
    }

    return result;
}

int main(){
    string a="wordgoodgoodgoodbestword";
    vector<string>w={"word","good","best","good"};
    vector<int>result=findSubstring(a,w);
    for(auto temp:result){
        cout<<temp<<",";
    }
    return 0;
}